import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:seedfour/app/constants.dart';
import 'package:seedfour/app/theme.dart';
import 'package:timeline_tile/timeline_tile.dart';

import 'logic.dart';

class FundDetailPage extends StatelessWidget {
  FundDetailPage({Key? key}) : super(key: key);

  final FundDetailLogic logic = Get.put(FundDetailLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('专款详情'),
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back_ios,
            size: 16.w,
          ),
          onPressed: () {
            Get.back();
          },
        ),
      ),
      body: Obx(() {
        if (logic.isLoading.value) {
          return const Center(child: CircularProgressIndicator());
        } else {
          return RefreshIndicator(
            onRefresh: () async => logic.reload(),
            child: ListView(
                padding: EdgeInsets.symmetric(
                  horizontal: kSpaceSmall,
                  vertical: kSpaceMedium,
                ),
                controller: logic.scrollController,
                children: [
                  /// 金额与操作
                  _buildModuleBg([
                    SizedBox(height: 20.w),
                    Text(
                      '当前金额(元)',
                      style: TextStyle(
                        fontSize: kFontSizeMini,
                        color: AppColor.colorFontBlack,
                      ),
                    ),
                    SizedBox(height: 6.w),
                    Text(
                      logic.balance.value,
                      style: TextStyle(
                        fontSize: kFontSizeHuge,
                        color: AppColor.colorFontBlack,
                        fontWeight: AppFont.fontExtraBold,
                      ),
                    ),
                    SizedBox(height: 20.w),
                    Row(
                      children: [
                        SizedBox(width: kSpaceMedium),
                        Expanded(
                          child: FilledButton(
                            style: FilledButton.styleFrom(
                              backgroundColor: AppColor.colorBtnGrey,
                              padding: EdgeInsets.symmetric(vertical: 14.w),
                              shape: RoundedRectangleBorder(
                                borderRadius:
                                    BorderRadius.circular(kRadiusMedium),
                              ),
                            ),
                            onPressed: () => logic.onClickWithdraw(),
                            child: Text(
                              '提现',
                              style: TextStyle(
                                fontSize: kFontSizeMedium,
                                color: AppColor.colorFontBlack,
                              ),
                            ),
                          ),
                        ),
                        SizedBox(width: kSpaceMedium),
                        Expanded(
                          child: FilledButton(
                            style: FilledButton.styleFrom(
                              backgroundColor: AppColor.colorFontBlue,
                              padding: EdgeInsets.symmetric(vertical: 14.w),
                              shape: RoundedRectangleBorder(
                                borderRadius:
                                    BorderRadius.circular(kRadiusMedium),
                              ),
                            ),
                            onPressed: () => logic.onClickTransfer(),
                            child: Text(
                              '转账',
                              style: TextStyle(
                                fontSize: kFontSizeMedium,
                                color: Colors.white,
                              ),
                            ),
                          ),
                        ),
                        SizedBox(width: kSpaceMedium),
                      ],
                    ),
                    SizedBox(height: 25.w),
                  ]),

                  SizedBox(height: kSpaceMedium),

                  /// 原始订单信息
                  _buildModuleBg([
                    Container(
                      width: double.maxFinite,
                      padding: EdgeInsets.all(kSpaceMedium),
                      child: Text(
                        '原始订单信息',
                        textAlign: TextAlign.start,
                        style: TextStyle(
                          fontSize: kFontSizeMedium,
                          color: AppColor.colorFontBlack,
                          fontWeight: AppFont.fontMedium,
                        ),
                      ),
                    ),
                    const Divider(),
                    SizedBox(height: kSpaceMedium),
                    _buildInfoItem('负责人', logic.orderInfo.value!.name ?? ''),
                    SizedBox(height: kSpaceMedium),
                    _buildInfoItem(
                        '分配规则', logic.orderInfo.value!.settleName ?? ''),
                    SizedBox(height: kSpaceMedium),
                    _buildInfoItem('专款总金额',
                        '¥${logic.allocDetail.value!.alloc!.amount}(${logic.allocDetail.value!.alloc!.percent}%)'),
                    SizedBox(height: kSpaceMedium),
                    _buildInfoItem(
                        '到账时间',
                        DateFormat(
                          'yyyy.MM.dd HH:mm',
                        ).format(DateTime.fromMillisecondsSinceEpoch(
                            logic.allocDetail.value!.createDate!))),
                    SizedBox(height: kSpaceMedium),
                    _buildInfoItem(
                        '对方户名', logic.allocDetail.value!.order!.title ?? ''),
                    SizedBox(height: kSpaceMedium),
                    _buildInfoItem('订单总金额',
                        '¥${logic.allocDetail.value!.order!.orderAmount}'),
                    SizedBox(height: kSpaceMedium),
                    Padding(
                      padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            '关联案件',
                            style: TextStyle(
                              fontSize: kFontSizeSmall,
                              color: AppColor.colorFontBlack,
                            ),
                          ),
                          SizedBox(width: kSpaceMedium),
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.end,
                              children: List.generate(
                                  logic.orderInfo.value!.invoice!.relateCases!
                                      .length, (index) {
                                return InkWell(
                                  onTap: () => logic.onClickCase(index),
                                  child: Text(
                                    logic.orderInfo.value!.invoice!
                                            .relateCases![index].caseName ??
                                        '',
                                    style: TextStyle(
                                      fontSize: kFontSizeSmall,
                                      color: AppColor.colorFontBlue,
                                    ),
                                  ),
                                );
                              }),
                            ),
                          ),
                        ],
                      ),
                    ),
                    SizedBox(height: kSpaceMedium),
                  ]),

                  SizedBox(height: kSpaceMedium),

                  /// 专款流水记录
                  _buildModuleBg([
                    Container(
                      width: double.maxFinite,
                      padding: EdgeInsets.all(kSpaceMedium),
                      child: Text(
                        '${logic.records.isEmpty ? '无' : ''}专款流水记录',
                        textAlign: TextAlign.start,
                        style: TextStyle(
                          fontSize: kFontSizeMedium,
                          color: AppColor.colorFontBlack,
                          fontWeight: AppFont.fontMedium,
                        ),
                      ),
                    ),
                    if (logic.records.isNotEmpty) ...[
                      const Divider(),
                      SizedBox(height: kSpaceMedium),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: List.generate(logic.records.length, (index) {
                          return _buildHistoryItem(
                            index: index,
                            item: logic.records[index],
                          );
                        }),
                      ),
                    ],
                  ]),
                ]),
          );
        }
      }),
    );
  }

  Widget _buildModuleBg(List<Widget> children) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(kRadiusMedium),
      ),
      child: Column(
        children: children,
      ),
    );
  }

  Widget _buildInfoItem(String title, String value) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: kFontSizeSmall,
              color: AppColor.colorFontBlack,
            ),
          ),
          SizedBox(width: kSpaceMedium),
          Expanded(
            child: Text(
              value,
              textAlign: TextAlign.right,
              style: TextStyle(
                fontSize: kFontSizeSmall,
                color: AppColor.colorFontBlack,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildHistoryItem({
    required int index,
    required FundDetailRecord item,
  }) {
    return Padding(
      padding: EdgeInsets.only(left: kSpaceMedium, right: kSpaceTiny),
      child: TimelineTile(
        alignment: TimelineAlign.start,
        isFirst: index == 0,
        isLast: index == logic.records.length - 1,
        beforeLineStyle: LineStyle(
          color: AppColor.colorLineGrey,
          thickness: 0.5.w,
        ),
        afterLineStyle: LineStyle(
          color: AppColor.colorLineGrey,
          thickness: 0.5.w,
        ),
        indicatorStyle: IndicatorStyle(
            indicatorXY: 0.2,
            indicator: SvgPicture.asset(
              'assets/icons/ic_timeline_tip.svg',
              width: 12.w,
              height: 12.w,
            )),
        endChild: Padding(
          padding: EdgeInsets.all(kSpaceSmall),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Expanded(
                    child: Text(
                      item.typeDesc,
                      style: TextStyle(
                        color: AppColor.colorFontBlack,
                        fontSize: kFontSizeSmall,
                        fontWeight: AppFont.fontMedium,
                      ),
                    ),
                  ),
                  SizedBox(width: kSpaceMedium),
                  Text(
                    item.amountStr,
                    style: TextStyle(
                      color: AppColor.colorFontBlack,
                      fontSize: kFontSizeMedium,
                    ),
                  ),
                ],
              ),
              Text(
                item.date,
                style: TextStyle(
                  color: AppColor.colorFontBlack,
                  fontSize: kFontSizeMini,
                ),
              ),
              SizedBox(height: kSpaceSmall),
              Container(
                padding: EdgeInsets.all(kSpaceSmall),
                decoration: BoxDecoration(
                  color: AppColor.colorRectangleGrey,
                  borderRadius: BorderRadius.circular(kRadiusMedium),
                ),
                child: Text(
                  item.remark,
                  style: TextStyle(
                    color: AppColor.colorFontBlack,
                    fontSize: kFontSizeSmall,
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
